/**
 *
 */
package ua.at.tsvetkov.samizdat.activities;

import ua.at.tsvetkov.util.Log;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.adapters.AdtAuthors;
import ua.at.tsvetkov.samizdat.data.Data;
import ua.at.tsvetkov.samizdat.data.DataDownloader;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AcAuthors extends AcTemplate {

	private static final int		DIALOG_ORDER_SELECT	= 3;
	private String						alphaCode;
	private int							authorsCount;
	//private String						sort;
	private String						urlStr;
	private String						fileName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		alphaCode = getIntent().getExtras().getString("code");
		try {
			authorsCount = Integer.parseInt(getIntent().getExtras().getString("count"));
		} catch (NumberFormatException e) {
			Log.e("Неправильное количество", e);
			finish();
		}
		// /cgi-bin/areader?q=alpha&anum=242&page=1&pagelen=20
		//sort = AppConfig.getSetting(Const.KEY_GENRE_BOOKS_SORTING);
		urlStr = Const.URL_ALPHABET_BOOKS + alphaCode + "&page=1&pagelen=" + authorsCount;
		fileName = Const.FILE_ROOT + '.' + alphaCode + "_alpha.txt";

		dataDownloader = new DataDownloader(this, (ProgressDialog) onCreateDialog(DIALOG_LOAD), urlStr, Const.LIST_VIEW_COUNT);
		startPosition = 0;
		dataDownloader.setOnCompleteLoadListener(standartNotifier);
		addListView();
		dataDownloader.download(authorsCount, fileName, true, false);
		adapter = new AdtAuthors(this, dataDownloader, Const.LIST_VIEW_COUNT);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (showMore(position, listView))
					return;
				String[] authorData = dataDownloader.getDataFields(position - 1, Data.AUTHOR_FIELDS_COUNT);
				Log.i("Selected " + authorData[Data.AUTHOR_OBJECT]);
				Intent intent = new Intent(AcAuthors.this, AcAuthorBooks.class);
				intent.putExtra("author", authorData[Data.AUTHOR_AUTHOR]);
				intent.putExtra("object", authorData[Data.AUTHOR_OBJECT]);
				intent.putExtra("count", authorData[Data.AUTHOR_COUNT]);
				startActivity(intent);
			}

		});
	}

	@Override
	protected void onResume() {
		adapter.refreshData();
		adapter.notifyDataSetChanged();
		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			showDialog(DIALOG_ORDER_SELECT);
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		}
		return false;
	}

}
